import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import { getCookie } from "../Cookies";
import { promptBialogbox } from "../Tihsi/tishi";

const routes = [
  {
    path: "/",
    component: () => import("@/Login/Login.vue"),
  },
  {
    path: "/home",
    component: () => import("../Home/Home.vue"),
    children: [
      {
        path: "/home/body",
        meta: {
          title: "首页",
        },
        component: () => import("../Menuitem/body.vue"),
      },
      {
        path: "/commodity/Commodity_classify",
        meta: {
          title: "商品分类",
        },
        component: () => import("../Menuitem/Commodity/Commodity_classify.vue"),
      },
      {
        path: "/commodity/Commodity_evaluate",
        meta: {
          title: "商品评价",
        },
        component: () => import("../Menuitem/Commodity/Commodity_evaluate.vue"),
      },
      {
        path: "/commodity/Commodity_list",
        meta: {
          title: "商品列表",
        },
        component: () => import("../Menuitem/Commodity/Commodity_list.vue"),
      },
      {
        path: "/data/OrderStatisyics",
        meta: {
          title: "订单统计",
        },
        component: () =>
          import("../Menuitem/DataStatistics/OrderStatisyics.vue"),
      },
      {
        path: "/data/ProductStatistics",
        meta: {
          title: "商品统计",
        },
        component: () =>
          import("../Menuitem/DataStatistics/ProductStatistics.vue"),
      },
      {
        path: "/data/UserStatistics",
        meta: {
          title: "用户统计",
        },
        component: () =>
          import("../Menuitem/DataStatistics/UserStatistics.vue"),
      },
      {
        path: "/information/Message",
        meta: {
          title: "消息通知",
        },
        component: () => import("../Menuitem/Information/Message.vue"),
      },
      {
        path: "/marketing/discount",
        meta: {
          title: "优惠券",
        },
        component: () => import("../Menuitem/Marketing/discount.vue"),
      },
      {
        path: "/order/AfterSale",
        meta: {
          title: "售后处理",
        },
        component: () => import("../Menuitem/OrderForm/AfterSale.vue"),
      },
      {
        path: "/order/OrderForm_List",
        meta: {
          title: "订单列表",
        },
        component: () => import("../Menuitem/OrderForm/OrderForm_List.vue"),
      },
      {
        path: "/store/Store_Classify",
        meta: {
          title: "分类导航",
        },
        component: () =>
          import("../Menuitem/Store_Management/Store_Classify.vue"),
      },
      {
        path: "/system/admin",
        meta: {
          title: "账号管理",
        },
        component: () => import("../Menuitem/System/admin.vue"),
      },
      {
        path: "/system/Jurisdiction",
        meta: {
          title: "操作权限",
        },
        component: () => import("../Menuitem/System/Jurisdiction.vue"),
      },
      {
        path: "/user/UserList",
        meta: {
          title: "用户列表",
        },
        component: () => import("../Menuitem/User/UserList.vue"),
      },
      {
        path: "/user/Detail",
        meta: {
          title: "用户详情",
        },
        component: () => import("../Detail/detail1.vue"),
      },
      {
        path: "/discount/Detail",
        meta: {
          title: "优惠券详情",
        },
        component: () => import("../Detail/detail2.vue"),
      },
      {
        path: "/discount/Detail3",
        meta: {
          title: "详情",
        },
        component: () => import("../Detail/detail3.vue"),
      },
      {
        path: "/discount/Detail4",
        meta: {
          title: "详情",
        },
        component: () => import("../Detail/detail4.vue"),
      },
      {
        path: "/discount/Detail5",
        meta: {
          title: "详情",
        },
        component: () => import("../Detail/detail5.vue"),
      },
      {
        path: "/discount/Detail6",
        meta: {
          title: "详情",
        },
        component: () => import("../Detail/detail6.vue"),
      },
    ],
  },
  {
    path: "/:pathMatch(.*)*",
    name: "",
    component: () => import("../404/L404.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
});

router.beforeEach(async (to, from, next) => {
  const token = getCookie();
  if (!token && to.path !== "/") {
    promptBialogbox("请先登录！", "error");
    return next("/");
  }
  if (token && to.path == "/") {
    promptBialogbox("请不要重新登录", "error");
    return next({
      path: from.path ? from.path : "/home",
    });
  }

  next();
});

export default router;
